package com.andyou.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.andyou.common.config.Global;
import com.andyou.common.mapper.JsonMapper;
import com.andyou.common.persistence.Page;
import com.andyou.common.utils.DateUtils;
import com.andyou.common.utils.GridVO;
import com.andyou.common.utils.excel.ExportExcel;
import com.andyou.common.web.BaseController;
import com.andyou.modules.sys.entity.Log;
import com.andyou.modules.sys.service.LogService;

/**
 * 日志Controller
 * @author ThinkGem
 * @version 2013-6-2
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/log")
public class LogController extends BaseController {

	@Autowired
	private LogService logService;
	

	@RequestMapping(value = {"list", ""})
	public String list(Log log, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Log> page = logService.findPage(new Page<Log>(request, response), log); 
        model.addAttribute("page", page);
		return "modules/sys/logList";
	}
	
	/**
	 * 验收入库列表信息
	 * 
	 * @param asset
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "export")
	public String exportFile(Log log, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			String fileName = "日志信息" + DateUtils.getDate("yyyyMMddHHmmss")
					+ ".xlsx";
			Page<Log> page_=new Page<Log>(request,response, -1);
			page_.setPageSize(10000);
			Page<Log> page = logService.findPage(page_, log);
			new ExportExcel("日志信息", Log.class).setDataList(page.getList())
					.write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出日志信息失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath() + "/sys/list";
	}

	/**
	 * 获取日志列表
	 * @param log
	 * @param request
	 * @param response
	 * @param model
	 * @return （参数是否可空/不可空）
	 * @author vikingbin
	 * Date:2017年1月4日；
	 */
	@ResponseBody
	@RequestMapping(value = { "logListJson" })
	public String logListJson(Log log, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		if ("1".equals(log.getSearchType())) {
			log.setTitle("系统登录");
		}else if("2".equals(log.getSearchType())){
			//不设置参数
		}else if("3".equals(log.getSearchType())){
			log.setTitle("-查看");
		}
	
		Page<Log> page = logService.findPage(new Page<Log>(request, response,true),
				log);
		model.addAttribute("page", page);

		GridVO gridVO = new GridVO();
		gridVO.setRows(page.getList());
		gridVO.setTotal(page.getCount());
		
		return JsonMapper.getInstance().toJson(gridVO);
	}
}
